package com.guli.third.service.impl;

import com.guli.common.to.SmsCode;
import com.guli.common.utils.R;
import com.guli.third.properties.SmsProperties;
import com.guli.third.service.SmsService;
import com.guli.third.utils.HttpUtils;
import org.apache.http.HttpResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author zzb
 * @Date 2024/5/29 15:43
 * @Version 1.0
 * @description
 */
@Service
public class SmsServiceImpl implements SmsService {
    @Autowired
    private SmsProperties smsProperties;

    @Override
    public R sendSmsCode(SmsCode smsCode) {
        Map<String, String> headers = new HashMap<>();
        //最后在header中的格式(中间是英文空格)为Authorization:APPCODE 83359fd73fe94948385f570e3c139105
        headers.put("Authorization", "APPCODE " + smsProperties.getAppcode());
        //根据API的要求，定义相对应的Content-Type
        headers.put("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
        Map<String, String> querys = new HashMap<>();
        Map<String, String> bodys = new HashMap<>();
        bodys.put("content", "code:" + smsCode.getCode());
        bodys.put("template_id", "CST_ptdie100");  //该模板为调试接口专用，短信下发有受限制，调试成功后请联系客服报备专属模板
        bodys.put("phone_number", "19066523452");
        try {
            HttpResponse response = HttpUtils.doPost(smsProperties.getHost(),
                    smsProperties.getPath(),
                    smsProperties.getMethod(),
                    headers,
                    querys,
                    bodys);
            System.out.println(response.getEntity());
            System.out.println(response.getLocale());
            System.out.println(response.getStatusLine());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return R.ok();
    }
}
